﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Hive.Core.Common.Shell
{
    /// <summary>
    /// Describes the characteristics of a shell command to run.
    /// </summary>
    public class ShellCommand
    {
        private readonly string filePath;
        private readonly string arguments;
        private readonly DirectoryInfo workingDirectory;

        /// <summary>
        /// Creates a shell command.
        /// </summary>
        /// <param name="filePath">The path of the file to run.</param>
        /// <param name="arguments">The command arguments.</param>
        /// <param name="workingDirectory">The working directory for the command.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="filePath"/>,
        /// <paramref name="arguments"/> or <paramref name="workingDirectory"/> is null.</exception>
        public ShellCommand(string filePath, string arguments, DirectoryInfo workingDirectory)
        {
            if (filePath == null)
                throw new ArgumentNullException("commandLine");
            if (arguments == null)
                throw new ArgumentNullException("arguments");
            if (workingDirectory == null)
                throw new ArgumentNullException("workingDirectory");

            this.filePath = filePath;
            this.arguments = arguments;
            this.workingDirectory = workingDirectory;
            Cancelable = true;
        }

        /// <summary>
        /// Gets the command line to run.
        /// </summary>
        public string FilePath
        {
            get { return filePath; }
        }

        /// <summary>
        /// Gets or sets the command arguments, or an empty string if none.
        /// </summary>
        public string Arguments
        {
            get { return arguments; }
        }

        /// <summary>
        /// Gets the working directory for the command.
        /// </summary>
        public DirectoryInfo WorkingDirectory
        {
            get { return workingDirectory; }
        }

        /// <summary>
        /// Gets or sets whether the command should be canceled if the shell is canceled.
        /// </summary>
        /// <value>True if the command may be canceled, false if the command will run
        /// to completion regardless of cancelation.  The default is true.</value>
        public bool Cancelable { get; set; }

        /// <summary>
        /// Converts the command to a string for display purposes.
        /// </summary>
        /// <returns>The command string.</returns>
        public override string ToString()
        {
            return string.Concat("\"", filePath, "\"", arguments.Length != 0 ? " " : string.Empty, arguments);
        }
    }
}
